#!/usr/bin/env python
"""
简单的Django测试脚本
"""

import os
import sys

# 设置Django环境
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'learn_drf.settings')

try:
    import django
    print("✅ Django导入成功")

    django.setup()
    print("✅ Django设置成功")

    # 测试导入应用模块
    try:
        from api_examples import models
        print("✅ models模块导入成功")
    except Exception as e:
        print(f"❌ models模块导入失败: {e}")

    try:
        from api_examples import serializers
        print("✅ serializers模块导入成功")
    except Exception as e:
        print(f"❌ serializers模块导入失败: {e}")

    try:
        from api_examples import views
        print("✅ views模块导入成功")
    except Exception as e:
        print(f"❌ views模块导入失败: {e}")

    # 测试创建模型实例
    try:
        from api_examples.models import Category
        print("✅ Category模型导入成功")

        # 检查数据库连接
        from django.db import connection
        with connection.cursor() as cursor:
            cursor.execute("SELECT 1")
            print("✅ 数据库连接正常")
    except Exception as e:
        print(f"❌ 数据库或模型测试失败: {e}")

except ImportError as e:
    print(f"❌ Django导入失败: {e}")
except Exception as e:
    print(f"❌ 其他错误: {e}")

print("\n测试完成")
